<?php
  MYDCONN();
  $PAYTYPE = 'null';
  $APROVAL = 'null';
  $PAYN = '';
  if ($_POST['CODE']=='') { echo '<div class="Error">Nao foi indicada conta.</div>'; die();}
  if ($_POST['Provider']=='') { echo '<div class="Error">Nao foi indicado fornecedor.</div>'; die();}
  if ($_POST['Office']=='') { echo '<div class="Error">Nao foi indicada uma loja.</div>'; die();}
  if ($_POST['Contract']=='') {$_POST['Contract'] = 0;}
  if (!isset($_POST['TimePay'])) {$_POST['TimePay'] = '0000-00-00';}
  if (!isset($_POST['TimeInvoice'])) {$_POST['TimeInvoice'] = '0000-00-00';}
  if (!isset($_POST['TimeReceived'])) { $_POST['TimeReceived'] = '0000-00-00';}
  if (!isset($_POST['TimeClose'])) { $_POST['TimeClose'] = '0000-00-00'; }
  if (!isset($_POST['TimeStart'])) {$_POST['TimeStart'] = '0000-00-00';}
  if (!isset($_POST['TimeEnd'])) {$_POST['TimeEnd'] = '0000-00-00';}

  if ($_POST['Status'] > 1) {
	$vdate = "'".$_POST['TimeClose']."'";
  } else {
	$vdate = 'null';
  }

  if (isset($_POST['Meth']) && isset($_POST['Status']) && $_POST['Status'] > 1) {
  $PAYTYPE = $_POST['Meth'];
  $APROVAL = 0;
  $PAYN = $_POST['PayData'];
  }
  if (isset($_POST['Description'])) {
  $_POST['Description'] = req_form($_POST['Description']);
  $_POST['ProvIReference'] = req_form($_POST['ProvIReference']);
  $_POST['ArtDescription'] = req_form($_POST['ArtDescription']);
  }

  if ($_POST['RefInvoice'] == 0) {

  $Verify = $GLOBALS['MYD']->db_array("SELECT ccenter_invoices.Ref, main_users.name FROM ccenter_invoices
        INNER JOIN main_users ON main_users.ref = ccenter_invoices.sys_User
        WHERE RefProvider = {$_POST['Provider']} AND
          (ProvIReference = '{$_POST['ProvIReference']}' AND ProvIReference != '')");
  if (!isset($Verify[1])) {
  $GLOBALS['MYD']->db_query("INSERT INTO ccenter_invoices
      (CODE, RefProvider, Amount, Status, TYPEPay, Time, TimeClose, TimeReceived, TimePay, TimeInvoice, Description, PayAprove, RefOffice, RefUser, sys_USER, ProvIReference, Observations, RefCurrency, TimeStart, TimeEnd, RefContract, PayData)
      VALUES
      ({$_POST['CODE']},{$_POST['Provider']},'{$_POST['Amount']}',{$_POST['Status']},{$PAYTYPE},'{$sysTime}',{$vdate},'{$_POST['TimeReceived']}','{$_POST['TimePay']}','{$_POST['TimeInvoice']}','{$_POST['Description']}',{$APROVAL},0,0,{$_SESSION[$SESSID]},'{$_POST['ProvIReference']}','{$_POST['Description']}',1,'{$_POST['TimeStart']}','{$_POST['TimeEnd']}', {$_POST['Contract']}, '{$PAYN}')");

  $Op = $GLOBALS['MYD']->db_array("SELECT Ref FROM ccenter_invoices
      ORDER BY Ref DESC LIMIT 0,1");

  $GLOBALS['MYD']->db_query("UPDATE ccenter_invarticles
      SET RefInvoice = {$Op[1]['Ref']}
      WHERE TmpInvoice = {$_POST['RefInvoice']} AND RefInvoice = 0 AND sysUser = {$_SESSION[$SESSID]}");
  } elseif (isset($Verify[1]['Ref'])) {
  echo '<script charset="utf-8" laguage="Javascript">alert(\'A Factura '.$Verify[1]['Ref'].' j� se encontra registada por '.$Verify[1]['name'].'\');</script>';
  }
  } elseif ($_POST['RefInvoice']>0) {
  $GLOBALS['MYD']->db_query("UPDATE ccenter_invoices
            SET CODE = {$_POST['CODE']},
            RefProvider = {$_POST['Provider']},
            RefContract = {$_POST['Contract']},
            Amount = '{$_POST['Amount']}',
            Status = {$_POST['Status']},
            TYPEPay = {$PAYTYPE},
            TimeClose = '{$_POST['TimeClose']}',
            TimeReceived = '{$_POST['TimeReceived']}',
            TimePay = '{$_POST['TimePay']}',
            TimeInvoice = '{$_POST['TimeInvoice']}',
            Description = '{$_POST['Description']}',
            PayAprove = {$APROVAL},
            RefOffice = 0,
            RefUser = 0,
            ProvIReference = '{$_POST['ProvIReference']}',
            Observations = '{$_POST['Description']}',
            RefCurrency = 1,
            TimeStart = '{$_POST['TimeStart']}',
            TimeEnd = '{$_POST['TimeEnd']}',
            PayData = '{$PAYN}'
            WHERE Ref = {$_POST['RefInvoice']}");
  }

  if (isset($_GET['ACT']) && $_GET['ACT'] == 'DT') {
  $GLOBALS['MYD']->db_query("UPDATE ccenter_invoices SET Status = 5 WHERE Ref = {$_POST['RefInvoice']}");
  $GLOBALS['MYD']->db_query("UPDATE ccenter_invarticles SET Status = 5 WHERE RefInvoice = {$_POST['RefInvoice']}");
  }

  if (!isset($Action)) {
   echo '<div class="Confirm">Opera��o Executada.</div>
   		<script charset="utf-8" type="text/javascript">document.edtCdcInvoice.cdcSubmit.disabled=true;</script>';
    } else {
  echo '<div class="Confirm">Erro, não foi possível concluír a operação.</div>';
    }